package com.nowoe.server.http;

import com.nowoe.server.constant.Http;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 倚栏听风 2024/1/5 15:13
 */
public class Request {
    private String method;
    private String uri;

    private InputStream inputStream;

    public Request() {
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        String headers = new String(bytes);
        if (headers.contains(Http.GET)) {
            this.method = Http.GET;
            this.uri = headers.substring(headers.indexOf(Http.GET) + Http.GET.length(), headers.indexOf(Http.HTTP)).trim();
        } else if (headers.contains(Http.POST)) {
            this.method = Http.POST;
            this.uri = headers.substring(headers.indexOf(Http.POST) + Http.POST.length(), headers.indexOf(Http.HTTP)).trim();
        }
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
